package com.jic.member.impl;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.rest.UserSysDeptApi;
import com.jic.member.constant.consist.ResponseCode;
import com.jic.member.exception.AppRuntimeException;
import com.jic.member.mapper.TGroupLabelRelationMapper;
import com.jic.member.mapper.TLabelMapper;
import com.jic.member.mapper.TLabelMemberRelationMapper;
import com.jic.member.mapper.TLabelMerchantRelationMapper;
import com.jic.member.entity.TGroupLabelRelation;
import com.jic.member.entity.TLabel;
import com.jic.member.entity.TLabelMemberRelation;
import com.jic.member.entity.TLabelMerchantRelation;
import com.jic.member.request.TGroupLabelRelationRequest;
import com.jic.member.request.TLabelAddedRequest;
import com.jic.member.response.TLabelGradeResponse;
import com.jic.member.response.TLabelResponse;
import com.jic.member.service.MememberUserService;
import com.jic.member.service.TLabelService;
import com.jic.manager.response.SysDeptResponse;
import com.jic.member.request.LabelRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 接口实现类
 *
 * @author : 建投数据科技(山东)有限公司
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class TLabelServiceImpl implements TLabelService {

    @Resource
    private TLabelMapper mapper;

    @Resource
    private TLabelMemberRelationMapper tLabelMemberRelationMapper;


    @Resource
    private TLabelMerchantRelationMapper tLabelMerchantRelationMapper;

    @Resource
    private TGroupLabelRelationMapper tGroupLabelRelationMapper;


    @Resource
    private UserSysDeptApi sysDeptApi;

    @Autowired
    private MememberUserService mememberUserService;



    /**
     * 新增记录
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存成功
     */
    @Override
    public int save(TLabel request) {
        int flag = mapper.insert(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
        }
    }


    /**
     * 保存或修改记录信息
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存或修改成功
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:09
     */
    @Override
    public int saveOrUpdate(TLabel request) {
        log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
        try {
            if (request == null) {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "入参为空");
            }
            if (request.getId() != null) {
                log.info("保存或修改记录信息，修改，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.updateByPrimaryKey(request);
                if (flag == 1) {
                    return 1;
                }
            } else {
                log.info("保存或修改记录信息，保存，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.insertSelective(request);
                if (flag == 1) {
                    return 1;
                } else {
                    throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改失败");
                }
            }
        } catch (Exception e) {
            log.error("用户保存或修改失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "用户保存或修改失败");
        }
        return -1;
    }

    /**
     * 删除记录
     *
     * @param request 需要删除,含有主键的入参实体类
     * @return int 是否删除成功
     */
    @Override
    public int deleteByPrimaryKey(TLabel request) {
        int flag = mapper.deleteByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除失败");
        }
    }


    /**
     * 逻辑删除记录
     *
     * @param request 含有主键的逻辑删除实体类
     * @return int 是否逻辑删除成功
     */
    @Override
    public int deleteByPrimaryKeyLogically(TLabel request) {
        request.setDeleteFlag(1);
        int flag = mapper.updateByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
        }
    }


    /**
     * 修改
     *
     * @param request
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(TLabel request) {
        try {
            log.info("修改记录");
            TLabel param = new TLabel();
            BeanUtils.copyProperties(request, param);
            int flag = mapper.updateByPrimaryKeySelective(param);
            if (flag == 1) {
                return 1;
            } else {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
            }
        } catch (Exception e) {
            log.error("修改记录失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
        }

    }

    /**
     * 记录详情
     *
     * @param request 查询的入参实体类
     * @return 数据库查询到的实体类
     */
    @Override
    public TLabel getByPrimaryKey(TLabel request) {
        return mapper.selectByPrimaryKey(request);
    }


    /**
     * 不分页查询
     *
     * @param request 不分页查询入参实体类
     * @return 数据库查到的实体类列表
     */
    @Override
    public List<TLabel> listAllRecord(TLabel request) {
        //分页处理
        request.setDeleteFlag(0);
        return mapper.selectBySelective(request);
    }

    /**
     * 分页查询
     *
     * @param request 分页查询入参实体类
     * @return 含有分页信息的实体类列表
     */
    @Override
    public PageResult<TLabelResponse> listPageBySelective(TLabel request, Page page) {
        //计算下标
        int startIndex = (page.getStart() - 1) * page.getLimit();
        //查询所有三级标签
        request.setLabelGrade(Long.valueOf(3));
        request.setLabelType(1);
        List<TLabel> list = mapper.listPageBySelective(request, startIndex, page.getLimit());
        List<TLabelResponse> responseList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(item -> {
                TLabelResponse tLabelResponse = new TLabelResponse();
                BeanUtils.copyProperties(item, tLabelResponse);
//                if (null != item.getDeptId()) {
//                    RestResult<String> restResult = sysDeptApi.queryDeptNameById(item.getDeptId());
//                    if (restResult != null) {
//                        tLabelResponse.setDeptName(restResult.getData());
//                    }
//                }
                //查询所有二级标签
                TLabel tLabel = new TLabel();
                tLabel.setId(item.getPid());
                TLabel tSecondLabel = mapper.selectByPrimaryKey(tLabel);
                if (tSecondLabel != null) {
                    tLabelResponse.setSecondLabelName(tSecondLabel.getLabelName());
                    tLabelResponse.setSecondLabelId(tSecondLabel.getId());
                    //查询一级标签
                    TLabel tLabel1 = new TLabel();
                    tLabel1.setId(tSecondLabel.getPid());
                    TLabel tFirstLabel = mapper.selectByPrimaryKey(tLabel1);
                    if (tFirstLabel != null) {
                        tLabelResponse.setFirstLabelName(tFirstLabel.getLabelName());
                    }
                }
                responseList.add(tLabelResponse);
            });
        }
        //分组排序处理
        List<TLabelResponse> responseList1 = new ArrayList<>();
        Map<String, String> tLabelMap = new HashMap<String, String>();
        if (CollectionUtils.isNotEmpty(responseList)) {
            for (TLabelResponse tLabelResponse : responseList) {
                tLabelMap.put(tLabelResponse.getSecondLabelName(), "");
            }
            for (String key : tLabelMap.keySet()) {
                for (TLabelResponse tLabelResponse : responseList) {
                    if (key.equals(tLabelResponse.getSecondLabelName())) {
                        responseList1.add(tLabelResponse);
                    }
                }
            }
        }
        Long count = mapper.listPageBySelectiveCount(request);
        PageResult<TLabelResponse> result = new PageResult<>();
        result.setTotal(count);
        result.setRows(responseList1);
        return result;
    }

    @Override
    public PageResult<TLabelResponse> queryCustomLabelPage(TLabel request, Page page) {
        //计算下标
        int startIndex = (page.getStart() - 1) * page.getLimit();
        //查询所有自定义标签
        request.setLabelType(2);
        List<TLabel> list = mapper.queryCustomLabelPage(request, startIndex, page.getLimit());
        List<TLabelResponse> responseList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(item -> {
                TLabelResponse tLabelResponse = new TLabelResponse();
                BeanUtils.copyProperties(item, tLabelResponse);
                TLabelMemberRelation tLabelMemberRelation = new TLabelMemberRelation();
                tLabelMemberRelation.setLabelId(item.getId());
                tLabelMemberRelation.setDeptId(item.getDeptId());
                List<TLabelMemberRelation> tLabelMemberRelations = tLabelMemberRelationMapper.selectBySelective(tLabelMemberRelation);
                tLabelResponse.setMarkTimes(tLabelMemberRelations.size());
//                if (null != item.getDeptId()) {
//                    RestResult<String> restResult = sysDeptApi.queryDeptNameById(item.getDeptId());
//                    if (restResult != null) {
//                        tLabelResponse.setDeptName(restResult.getData());
//                    }
//                }
                responseList.add(tLabelResponse);
            });
        }
        Long count = mapper.queryCustomLabelPageCount(request);
        PageResult<TLabelResponse> result = new PageResult<>();
        result.setTotal(count);
        result.setRows(responseList);
        return result;
    }

    /**
     * 子平台入驻默认分配母平台全部标签
     *
     * @param deptId 保存入参
     * @return true 保存成功
     * @auther 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:58:44
     */
    @Override
    public RestResult saveLabelForSubPlatformEntry(Long deptId) {
        log.info("子平台入驻默认分配母平台全部标签，入参 = {}", JSONObject.toJSONString(deptId));
        try {
            //查询母平台所有一级标签
            TLabel tFirstLabel = new TLabel();
            tFirstLabel.setDeptType(0);
            tFirstLabel.setDeleteFlag(0);
            tFirstLabel.setLabelGrade(Long.valueOf(1));
            List<TLabel> tFirsetLabelList = mapper.selectBySelective(tFirstLabel);
            if (CollectionUtils.isNotEmpty(tFirsetLabelList)) {
                tFirsetLabelList.forEach(item1 -> {
                    TLabel tLabelCopy1 = new TLabel();
                    BeanUtils.copyProperties(item1, tLabelCopy1);
                    tLabelCopy1.setDeptId(deptId);
                    tLabelCopy1.setDeptType(1);
                    mapper.insert(tLabelCopy1);
                    //查询所有的二级标签
                    TLabel tSecondLabel = new TLabel();
                    tSecondLabel.setDeleteFlag(0);
                    tSecondLabel.setPid(item1.getId());
                    List<TLabel> tSecondLabelList = mapper.selectBySelective(tSecondLabel);
                    if (CollectionUtils.isNotEmpty(tSecondLabelList)) {
                        tSecondLabelList.forEach(item2 -> {
                            if (item2.getLabelName().equals("会员等级")) {
                                TLabel tLabelCopyMemberGrade = new TLabel();
                                BeanUtils.copyProperties(item2, tLabelCopyMemberGrade);
                                tLabelCopyMemberGrade.setPid(tLabelCopy1.getId());
                                tLabelCopyMemberGrade.setDeptType(1);
                                tLabelCopyMemberGrade.setDeptId(deptId);
                                mapper.insert(tLabelCopyMemberGrade);
                            } else {
                                TLabel tLabelCopy2 = new TLabel();
                                BeanUtils.copyProperties(item2, tLabelCopy2);
                                tLabelCopy2.setDeptId(deptId);
                                tLabelCopy2.setDeptType(1);
                                tLabelCopy2.setPid(tLabelCopy1.getId());
                                mapper.insert(tLabelCopy2);
                                //查询所有三级标签
                                TLabel tThirdLabel = new TLabel();
                                tThirdLabel.setDeleteFlag(0);
                                tThirdLabel.setPid(item2.getId());
                                List<TLabel> tThirtdLabelList = mapper.selectBySelective(tThirdLabel);
                                if (CollectionUtils.isNotEmpty(tThirtdLabelList)) {
                                    tThirtdLabelList.forEach(item3 -> {
                                        TLabel tLabelCopy3 = new TLabel();
                                        BeanUtils.copyProperties(item3, tLabelCopy3);
                                        tLabelCopy3.setDeptId(deptId);
                                        tLabelCopy3.setDeptType(1);
                                        tLabelCopy3.setPid(tLabelCopy2.getId());
                                        mapper.insert(tLabelCopy3);
                                    });
                                }
                            }
                        });
                    }
                });
            }
            return RestResult.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("分配标签失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "分配标签失败");
        }

    }


    /**
     * 会员等级值新增修改
     *
     * @param map 保存入参
     * @return true 保存成功
     * @auther 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:58:44
     */
    @Override
    public RestResult saveLabelMemberGradeValue(Map<String, Object> map) {
        if (!map.isEmpty()) {
            //获取当前部门的会员等级id
            TLabel tLabel = new TLabel();
            tLabel.setLabelName("会员等级");
            tLabel.setDeptId(Long.valueOf(map.get("deptId").toString()));
            List<TLabel> labelList = mapper.selectBySelective(tLabel);
            //存在
            if (CollectionUtils.isNotEmpty(labelList)) {
                //判断编辑还是新增
                TLabel tLabel1 = new TLabel();
                tLabel1.setLabelEnum(Long.valueOf(map.get("id").toString()));
                tLabel1.setPid(labelList.get(0).getId());
                tLabel1.setDeptId(Long.valueOf(map.get("deptId").toString()));
                List<TLabel> labelList1 = mapper.selectBySelective(tLabel1);
                //编辑
                if (CollectionUtils.isNotEmpty(labelList1)) {
                    TLabel tLabelMemberGradeValue = new TLabel();
                    tLabelMemberGradeValue.setLabelName(map.get("levelName").toString());
                    tLabelMemberGradeValue.setId(labelList1.get(0).getId());
                    mapper.updateByPrimaryKey(tLabelMemberGradeValue);
                } else {
                    TLabel tLabelMemberGradeValue = new TLabel();
                    tLabelMemberGradeValue.setLabelName(map.get("levelName").toString());
                    tLabelMemberGradeValue.setPid(labelList.get(0).getId());
                    tLabelMemberGradeValue.setStatus(2);
                    tLabelMemberGradeValue.setLabelGrade(Long.valueOf(3));
                    tLabelMemberGradeValue.setDeptType(Integer.valueOf(map.get("deptType").toString()));
                    tLabelMemberGradeValue.setLabelType(1);
                    tLabelMemberGradeValue.setDeptId(Long.valueOf(map.get("deptId").toString()));
                    tLabelMemberGradeValue.setLabelEnum(Long.valueOf(map.get("id").toString()));
                    mapper.insert(tLabelMemberGradeValue);
                }
            } else {
                //默认插入一条会员等级
                TLabel tLabelMemberGrade = new TLabel();
                tLabelMemberGrade.setLabelName("会员等级");
                tLabelMemberGrade.setStatus(2);
                tLabelMemberGrade.setLabelGrade(Long.valueOf(2));
                tLabelMemberGrade.setDeptType(Integer.valueOf(map.get("deptType").toString()));
                tLabelMemberGrade.setLabelType(1);
                tLabelMemberGrade.setDeptId(Long.valueOf(map.get("deptId").toString()));
                tLabelMemberGrade.setLabelEnum(Long.valueOf(map.get("id").toString()));
                mapper.insert(tLabelMemberGrade);

                TLabel tLabelMemberGradeValue = new TLabel();
                tLabelMemberGradeValue.setLabelName(map.get("levelName").toString());
                tLabelMemberGradeValue.setPid(tLabelMemberGrade.getId());
                tLabelMemberGradeValue.setStatus(2);
                tLabelMemberGradeValue.setLabelGrade(Long.valueOf(3));
                tLabelMemberGradeValue.setDeptType(Integer.valueOf(map.get("deptType").toString()));
                tLabelMemberGradeValue.setLabelType(1);
                tLabelMemberGradeValue.setDeptId(Long.valueOf(map.get("deptId").toString()));
                tLabelMemberGradeValue.setLabelEnum(Long.valueOf(map.get("id").toString()));
                mapper.insert(tLabelMemberGradeValue);
            }
        }
        return RestResult.success(Boolean.TRUE);
    }

    @Override
    public RestResult<List<TLabelGradeResponse>> queryLabelByDeptId(TGroupLabelRelationRequest tGroupLabelRelationRequest) {
        List<TLabelGradeResponse> responseList = new ArrayList<>();
        if (tGroupLabelRelationRequest.getAdminOperate().getDeptType().getCode() == 0 || tGroupLabelRelationRequest.getAdminOperate().getDeptType().getCode() == 1) {
            //查询平台的一级标签
            TLabel tFirstLabel = new TLabel();
            tFirstLabel.setDeleteFlag(0);
            if (null != tGroupLabelRelationRequest.getDeptId()) {
                tFirstLabel.setDeptId(tGroupLabelRelationRequest.getDeptId());
            } else {
                tFirstLabel.setDeptId(tGroupLabelRelationRequest.getAdminOperate().getDeptId());
            }
            tFirstLabel.setLabelGrade(Long.valueOf(1));
            List<TLabel> labelFirstList = mapper.selectBySelective(tFirstLabel);
            if (CollectionUtils.isNotEmpty(labelFirstList)) {
                for (TLabel tLabel : labelFirstList) {
                    TLabelGradeResponse tLabelFirstGradeResponse = new TLabelGradeResponse();
                    BeanUtils.copyProperties(tLabel, tLabelFirstGradeResponse);
                    responseList.add(tLabelFirstGradeResponse);
                    //查询二级标签
                    TLabel tSecondLabel = new TLabel();
                    tSecondLabel.setDeleteFlag(0);
                    tSecondLabel.setPid(tLabel.getId());
                    List<TLabel> labelSecondList = mapper.selectBySelective(tSecondLabel);
                    if (CollectionUtils.isNotEmpty(labelSecondList)) {
                        List<TLabelGradeResponse> responseSecondList = new ArrayList<>();
                        for (TLabel tLabel1 : labelSecondList) {
                            TLabelGradeResponse tLabelSecondGradeResponse = new TLabelGradeResponse();
                            BeanUtils.copyProperties(tLabel1, tLabelSecondGradeResponse);
                            responseSecondList.add(tLabelSecondGradeResponse);
                        }
                        tLabelFirstGradeResponse.setChildren(responseSecondList);
                    }
                }
            }
        } else {
//            //获取所有开放得标签
            TLabelMerchantRelation tLabelMerchantRelation = new TLabelMerchantRelation();
            if (null != tGroupLabelRelationRequest.getDeptId()) {
                tLabelMerchantRelation.setDeptId(tGroupLabelRelationRequest.getDeptId());
            } else {
                tLabelMerchantRelation.setDeptId(tGroupLabelRelationRequest.getAdminOperate().getDeptId());
            }
            tLabelMerchantRelation.setDeleteFlag(0);
            List<TLabelMerchantRelation> tLabelMerchantRelations = tLabelMerchantRelationMapper.selectBySelective(tLabelMerchantRelation);
            List<TLabel> labelFirstList = new ArrayList<>();
            List<TLabel> labelSecondList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(tLabelMerchantRelations)) {
                tLabelMerchantRelations.forEach(item -> {
                    TLabel tLabel = new TLabel();
                    tLabel.setId(item.getLabelId());
                    TLabel tLabel1 = mapper.selectByPrimaryKey(tLabel);
                    labelSecondList.add(tLabel1);
                    //根据二级标签获取一级标签
                    TLabel tLabel2 = new TLabel();
                    tLabel2.setId(tLabel1.getPid());
                    TLabel tLabel3 = mapper.selectByPrimaryKey(tLabel2);
                    labelFirstList.add(tLabel3);
                });
            }
            //去重
            List<TLabel> labelFirstList1 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(labelFirstList)) {
                if (CollectionUtils.isNotEmpty(labelFirstList)) {
                    labelFirstList1 = labelFirstList.stream().collect(Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TLabel::getId))), ArrayList::new)
                    );
                }
            }
            if (CollectionUtils.isNotEmpty(labelFirstList1)) {
                for (TLabel tLabel : labelFirstList1) {
                    TLabelGradeResponse tLabelFirstGradeResponse = new TLabelGradeResponse();
                    BeanUtils.copyProperties(tLabel, tLabelFirstGradeResponse);
                    responseList.add(tLabelFirstGradeResponse);
                    if (CollectionUtils.isNotEmpty(labelSecondList)) {
                        List<TLabelGradeResponse> responseSecondList = new ArrayList<>();
                        for (TLabel tLabel1 : labelSecondList) {
                            TLabelGradeResponse tLabelSecondGradeResponse = new TLabelGradeResponse();
                            BeanUtils.copyProperties(tLabel1, tLabelSecondGradeResponse);
                            responseSecondList.add(tLabelSecondGradeResponse);
                        }
                        //判断当前部门是否村则会员等级
                        TLabel memberGradeLabel = new TLabel();
                        memberGradeLabel.setLabelName("会员等级");
                        memberGradeLabel.setLabelGrade(Long.valueOf(2));
                        memberGradeLabel.setDeptId(tLabelMerchantRelation.getDeptId());
                        List<TLabel> memberGradeList = mapper.selectBySelective(memberGradeLabel);
                        if (CollectionUtils.isNotEmpty(memberGradeList)) {
                            TLabelGradeResponse tLabelGradeResponse = new TLabelGradeResponse();
                            BeanUtils.copyProperties(memberGradeList.get(0), tLabelGradeResponse);
                            responseSecondList.add(tLabelGradeResponse);
                        }
                        tLabelFirstGradeResponse.setChildren(responseSecondList);
                    }
                }
            }
        }

        return RestResult.success(responseList);
    }


    @Override
    public RestResult<List<TLabelGradeResponse>> queryGroupLabelByDeptId(TGroupLabelRelationRequest tGroupLabelRelationRequest) {
        List<TLabelGradeResponse> responseSecondList = new ArrayList<>();
        if (tGroupLabelRelationRequest.getAdminOperate().getDeptType().getCode() == 0 || tGroupLabelRelationRequest.getAdminOperate().getDeptType().getCode() == 1) {
            //查询平台的二级标签
            TLabel tFirstLabel = new TLabel();
            tFirstLabel.setDeleteFlag(0);
            if (null != tGroupLabelRelationRequest.getDeptId()) {
                tFirstLabel.setDeptId(tGroupLabelRelationRequest.getDeptId());
            } else {
                tFirstLabel.setDeptId(tGroupLabelRelationRequest.getAdminOperate().getDeptId());
            }
            tFirstLabel.setLabelGrade(Long.valueOf(2));
            List<TLabel> labelSecondList = mapper.selectBySelective(tFirstLabel);
            if (CollectionUtils.isNotEmpty(labelSecondList)) {
                for (TLabel tLabel1 : labelSecondList) {
                    TLabelGradeResponse tLabelSecondGradeResponse = new TLabelGradeResponse();
                    BeanUtils.copyProperties(tLabel1, tLabelSecondGradeResponse);
                    responseSecondList.add(tLabelSecondGradeResponse);
                    //查询三级标签
                    TLabel tThirdLabel = new TLabel();
                    tThirdLabel.setDeleteFlag(0);
                    tThirdLabel.setPid(tLabel1.getId());
                    List<TLabel> labelThirdList = mapper.selectBySelective(tThirdLabel);
                    if (CollectionUtils.isNotEmpty(labelThirdList)) {
                        List<TLabelGradeResponse> responseThirdList = new ArrayList<>();
                        for (TLabel tLabel2 : labelThirdList) {
                            TLabelGradeResponse tLabelThirdGradeResponse = new TLabelGradeResponse();
                            BeanUtils.copyProperties(tLabel2, tLabelThirdGradeResponse);
                            //判断当前三级标签是否选中
                            if (tGroupLabelRelationRequest.getUserGroupId() != null) {
                                TGroupLabelRelation tGroupLabelRelation = new TGroupLabelRelation();
                                tGroupLabelRelation.setLabelId(tLabel2.getId());
                                tGroupLabelRelation.setDeptId(tLabel2.getDeptId());
                                tGroupLabelRelation.setDeleteFlag(0);
                                tGroupLabelRelation.setUserGroupId(tGroupLabelRelationRequest.getUserGroupId());
                                List<TGroupLabelRelation> tGroupLabelRelations = tGroupLabelRelationMapper.selectBySelective(tGroupLabelRelation);
                                if (tGroupLabelRelations.size() == 0) {
                                    tLabelThirdGradeResponse.setIsChecked(Boolean.FALSE);
                                } else {
                                    tLabelThirdGradeResponse.setIsChecked(Boolean.TRUE);
                                }
                            } else {
                                tLabelThirdGradeResponse.setIsChecked(Boolean.FALSE);
                            }

                            responseThirdList.add(tLabelThirdGradeResponse);
                        }
                        tLabelSecondGradeResponse.setChildren(responseThirdList);
                    }
                }
            }
        } else {
            //获取所有开放得标签
            TLabelMerchantRelation tLabelMerchantRelation = new TLabelMerchantRelation();
            if (null != tGroupLabelRelationRequest.getDeptId()) {
                tLabelMerchantRelation.setDeptId(tGroupLabelRelationRequest.getDeptId());
            } else {
                tLabelMerchantRelation.setDeptId(tGroupLabelRelationRequest.getAdminOperate().getDeptId());
            }
            tLabelMerchantRelation.setDeleteFlag(0);
            List<TLabelMerchantRelation> tLabelMerchantRelations = tLabelMerchantRelationMapper.selectBySelective(tLabelMerchantRelation);
            List<TLabel> labelSecondList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(tLabelMerchantRelations)) {
                tLabelMerchantRelations.forEach(item -> {
                    TLabel tLabel = new TLabel();
                    tLabel.setId(item.getLabelId());
                    TLabel tLabel1 = mapper.selectByPrimaryKey(tLabel);
                    labelSecondList.add(tLabel1);
                });
            }
            //判断当前部门是否村则会员等级
            TLabel memberGradeLabel = new TLabel();
            memberGradeLabel.setLabelName("会员等级");
            memberGradeLabel.setLabelGrade(Long.valueOf(2));
            memberGradeLabel.setDeptId(tLabelMerchantRelation.getDeptId());
            List<TLabel> memberGradeList = mapper.selectBySelective(memberGradeLabel);
            if (CollectionUtils.isNotEmpty(memberGradeList)) {
                labelSecondList.add(memberGradeList.get(0));
            }
            if (CollectionUtils.isNotEmpty(labelSecondList)) {
                for (TLabel tLabel1 : labelSecondList) {
                    TLabelGradeResponse tLabelSecondGradeResponse = new TLabelGradeResponse();
                    BeanUtils.copyProperties(tLabel1, tLabelSecondGradeResponse);
                    responseSecondList.add(tLabelSecondGradeResponse);
                    //查询三级标签
                    TLabel tThirdLabel = new TLabel();
                    tThirdLabel.setDeleteFlag(0);
                    tThirdLabel.setPid(tLabel1.getId());
                    List<TLabel> labelThirdList = mapper.selectBySelective(tThirdLabel);
                    if (CollectionUtils.isNotEmpty(labelThirdList)) {
                        List<TLabelGradeResponse> responseThirdList = new ArrayList<>();
                        for (TLabel tLabel2 : labelThirdList) {
                            TLabelGradeResponse tLabelThirdGradeResponse = new TLabelGradeResponse();
                            BeanUtils.copyProperties(tLabel2, tLabelThirdGradeResponse);
                            //判断当前三级标签是否选中
                            if (tGroupLabelRelationRequest.getUserGroupId() != null) {
                                TGroupLabelRelation tGroupLabelRelation = new TGroupLabelRelation();
                                tGroupLabelRelation.setLabelId(tLabel2.getId());
                                tGroupLabelRelation.setDeptId(tGroupLabelRelationRequest.getDeptId());
                                tGroupLabelRelation.setDeleteFlag(0);
                                tGroupLabelRelation.setUserGroupId(tGroupLabelRelationRequest.getUserGroupId());
                                List<TGroupLabelRelation> tGroupLabelRelations = tGroupLabelRelationMapper.selectBySelective(tGroupLabelRelation);
                                if (tGroupLabelRelations.size() == 0) {
                                    tLabelThirdGradeResponse.setIsChecked(Boolean.FALSE);
                                } else {
                                    tLabelThirdGradeResponse.setIsChecked(Boolean.TRUE);
                                }
                            } else {
                                tLabelThirdGradeResponse.setIsChecked(Boolean.FALSE);
                            }

                            responseThirdList.add(tLabelThirdGradeResponse);
                        }
                        tLabelSecondGradeResponse.setChildren(responseThirdList);
                    }
                }
            }
        }
        return RestResult.success(responseSecondList);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult<Boolean> getMemberInfoBySex() {
        List<SysDeptResponse> deptList = sysDeptApi.getAllDeptList();
        try {
            if (CollectionUtils.isNotEmpty(deptList)) {
                deptList.forEach(item -> {
                    //子平台母平台
                    if (item.getDeptType() == 0 || item.getDeptType() == 1) {
                        TLabel tLabel = new TLabel();
                        tLabel.setDeptId(item.getId());
                        tLabel.setLabelName("性别");
                        List<TLabel> labelEnums = mapper.selectLabelEnumBySecondLabelName(tLabel);
                        labelEnums.forEach(item1 -> {
                            LabelRequest labelRequest = new LabelRequest();
                            labelRequest.setLabelType(1);
                            labelRequest.setDeptId(item.getId());
                            labelRequest.setLabelEnum(Long.valueOf(item1.getLabelEnum()));
                            //调用会员数据获取会员id
                            List<String> memeberList = mememberUserService.queryByLabelType(labelRequest);
                            //先删除已有的数据，重新保存
                            TLabelMemberRelation tLabelMemberRelation = new TLabelMemberRelation();
                            tLabelMemberRelation.setDeptId(item.getId());
                            tLabelMemberRelation.setLabelId(item1.getId());
                            tLabelMemberRelationMapper.deleteByDeptIdAndMemIdAndLabelId(tLabelMemberRelation);
                            memeberList.forEach(item2 -> {

                                TLabelMemberRelation tLabelMemberRelation1 = new TLabelMemberRelation();
                                tLabelMemberRelation1.setDeptId(item.getId());
                                tLabelMemberRelation1.setLabelId(item1.getId());
                                tLabelMemberRelation1.setMemberId(item2);
                                tLabelMemberRelationMapper.insert(tLabelMemberRelation1);
                            });
                        });
                    } else {
                        //商家店铺
                        TLabelMerchantRelation tLabelMerchantRelation = new TLabelMerchantRelation();
                        tLabelMerchantRelation.setDeptId(item.getId());
                        List<TLabelMerchantRelation> tLabelMerchantRelations = tLabelMerchantRelationMapper.selectBySelective(tLabelMerchantRelation);
                        if (CollectionUtils.isNotEmpty(tLabelMerchantRelations)) {
                            tLabelMerchantRelations.forEach(item1 -> {
                                LabelRequest labelRequest = new LabelRequest();
                                labelRequest.setLabelType(1);
                                labelRequest.setDeptId(item.getId());
                                labelRequest.setLabelEnum(Long.valueOf(item1.getLabelId()));
                                //调用会员数据获取会员id
                                List<String> memeberList = mememberUserService.queryByLabelType(labelRequest);
                                //先删除已有的数据，重新保存
                                TLabelMemberRelation tLabelMemberRelation = new TLabelMemberRelation();
                                tLabelMemberRelation.setDeptId(item.getId());
                                tLabelMemberRelation.setLabelId(item1.getLabelId());
                                tLabelMemberRelationMapper.deleteByDeptIdAndMemIdAndLabelId(tLabelMemberRelation);
                                memeberList.forEach(item2 -> {
                                    TLabelMemberRelation tLabelMemberRelation1 = new TLabelMemberRelation();
                                    tLabelMemberRelation1.setDeptId(item.getId());
                                    tLabelMemberRelation1.setLabelId(item1.getLabelId());
                                    tLabelMemberRelation1.setMemberId(item2);
                                    tLabelMemberRelationMapper.insert(tLabelMemberRelation1);
                                });
                            });
                        }
                    }
                });
            }
            return RestResult.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("会员标签数据同步失败:{}", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "会员标签数据同步失败");
        }

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult<Boolean> getMemberInfoByMemberGrade() {
        List<SysDeptResponse> deptList = sysDeptApi.getAllDeptList();
        try {
            if (CollectionUtils.isNotEmpty(deptList)) {
                deptList.forEach(item -> {
                    //子平台母平台
                    if (item.getDeptType() == 0 || item.getDeptType() == 1) {
                        TLabel tLabel = new TLabel();
                        tLabel.setDeptId(item.getId());
                        tLabel.setLabelName("会员等级");
                        List<TLabel> labelEnums = mapper.selectLabelEnumBySecondLabelName(tLabel);
                        labelEnums.forEach(item1 -> {
                            LabelRequest labelRequest = new LabelRequest();
                            labelRequest.setLabelType(2);
                            labelRequest.setDeptId(item.getId());
                            labelRequest.setLabelEnum(Long.valueOf(item1.getLabelEnum()));
                            //调用会员数据获取会员id
                            List<String> memeberList = mememberUserService.queryByLabelType(labelRequest);
                            //先删除已有的数据，重新保存
                            TLabelMemberRelation tLabelMemberRelation = new TLabelMemberRelation();
                            tLabelMemberRelation.setDeptId(item.getId());
                            tLabelMemberRelation.setLabelId(item1.getId());
                            tLabelMemberRelationMapper.deleteByDeptIdAndMemIdAndLabelId(tLabelMemberRelation);
                            memeberList.forEach(item2 -> {
                                TLabelMemberRelation tLabelMemberRelation1 = new TLabelMemberRelation();
                                tLabelMemberRelation1.setDeptId(item.getId());
                                tLabelMemberRelation1.setLabelId(item1.getId());
                                tLabelMemberRelation1.setMemberId(item2);
                                tLabelMemberRelationMapper.insert(tLabelMemberRelation1);
                            });
                        });
                    } else {
                        //商家店铺
                        TLabelMerchantRelation tLabelMerchantRelation = new TLabelMerchantRelation();
                        tLabelMerchantRelation.setDeptId(item.getId());
                        List<TLabelMerchantRelation> tLabelMerchantRelations = tLabelMerchantRelationMapper.selectBySelective(tLabelMerchantRelation);
                        //判断当前部门是否村则会员等级
                        TLabel memberGradeLabel = new TLabel();
                        memberGradeLabel.setLabelName("会员等级");
                        memberGradeLabel.setLabelGrade(Long.valueOf(2));
                        memberGradeLabel.setDeptId(tLabelMerchantRelation.getDeptId());
                        List<TLabel> memberGradeList = mapper.selectBySelective(memberGradeLabel);
                        if (CollectionUtils.isNotEmpty(memberGradeList)) {
                            TLabel tLabel = new TLabel();
                            tLabel.setPid(memberGradeList.get(0).getId());
                            List<TLabel> labelList = mapper.selectBySelective(tLabel);
                            if (CollectionUtils.isNotEmpty(labelList)) {
                                labelList.forEach(item2 -> {
                                    TLabelMerchantRelation tLabelMerchantRelation1 = new TLabelMerchantRelation();
                                    tLabelMerchantRelation1.setLabelId(item.getId());
                                    tLabelMerchantRelations.add(tLabelMerchantRelation1);
                                });
                            }
                        }
                        if (CollectionUtils.isNotEmpty(tLabelMerchantRelations)) {
                            tLabelMerchantRelations.forEach(item1 -> {
                                LabelRequest labelRequest = new LabelRequest();
                                labelRequest.setLabelType(2);
                                labelRequest.setDeptId(item.getId());
                                labelRequest.setLabelEnum(Long.valueOf(item1.getLabelId()));
                                //调用会员数据获取会员id
                                List<String> memeberList = mememberUserService.queryByLabelType(labelRequest);
                                //先删除已有的数据，重新保存
                                TLabelMemberRelation tLabelMemberRelation = new TLabelMemberRelation();
                                tLabelMemberRelation.setDeptId(item.getId());
                                tLabelMemberRelation.setLabelId(item1.getLabelId());
                                tLabelMemberRelationMapper.deleteByDeptIdAndMemIdAndLabelId(tLabelMemberRelation);
                                memeberList.forEach(item2 -> {
                                    TLabelMemberRelation tLabelMemberRelation1 = new TLabelMemberRelation();
                                    tLabelMemberRelation1.setDeptId(item.getId());
                                    tLabelMemberRelation1.setLabelId(item1.getLabelId());
                                    tLabelMemberRelation1.setMemberId(item2);
                                    tLabelMemberRelationMapper.insert(tLabelMemberRelation1);
                                });
                            });
                        }
                    }
                });
            }
            return RestResult.success(Boolean.TRUE);
        } catch (Exception e) {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "会员标签数据同步失败");
        }

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult<Boolean> getMemberInfoByMemberType() {
        List<SysDeptResponse> deptList = sysDeptApi.getAllDeptList();
        try {
            if (CollectionUtils.isNotEmpty(deptList)) {
                deptList.forEach(item -> {
                    //子平台母平台
                    if (item.getDeptType() == 0 || item.getDeptType() == 1) {
                        TLabel tLabel = new TLabel();
                        tLabel.setDeptId(item.getId());
                        tLabel.setLabelName("会员类型");
                        List<TLabel> labelEnums = mapper.selectLabelEnumBySecondLabelName(tLabel);
                        labelEnums.forEach(item1 -> {
                            LabelRequest labelRequest = new LabelRequest();
                            labelRequest.setLabelType(3);
                            labelRequest.setDeptId(item.getId());
                            labelRequest.setLabelEnum(Long.valueOf(item1.getLabelEnum()));
                            //调用会员数据获取会员id
                            List<String> memeberList = mememberUserService.queryByLabelType(labelRequest);
                            //先删除已有的数据，重新保存
                            TLabelMemberRelation tLabelMemberRelation = new TLabelMemberRelation();
                            tLabelMemberRelation.setDeptId(item.getId());
                            tLabelMemberRelation.setLabelId(item1.getId());
                            tLabelMemberRelationMapper.deleteByDeptIdAndMemIdAndLabelId(tLabelMemberRelation);
                            memeberList.forEach(item2 -> {
                                TLabelMemberRelation tLabelMemberRelation1 = new TLabelMemberRelation();
                                tLabelMemberRelation1.setDeptId(item.getId());
                                tLabelMemberRelation1.setLabelId(item1.getId());
                                tLabelMemberRelation1.setMemberId(item2);
                                tLabelMemberRelationMapper.insert(tLabelMemberRelation1);
                            });
                        });
                    } else {
                        //商家店铺
                        TLabelMerchantRelation tLabelMerchantRelation = new TLabelMerchantRelation();
                        tLabelMerchantRelation.setDeptId(item.getId());
                        List<TLabelMerchantRelation> tLabelMerchantRelations = tLabelMerchantRelationMapper.selectBySelective(tLabelMerchantRelation);
                        if (CollectionUtils.isNotEmpty(tLabelMerchantRelations)) {
                            tLabelMerchantRelations.forEach(item1 -> {
                                LabelRequest labelRequest = new LabelRequest();
                                labelRequest.setLabelType(3);
                                labelRequest.setDeptId(item.getId());
                                labelRequest.setLabelEnum(Long.valueOf(item1.getLabelId()));
                                //调用会员数据获取会员id
                                List<String> memeberList = mememberUserService.queryByLabelType(labelRequest);
                                //先删除已有的数据，重新保存
                                TLabelMemberRelation tLabelMemberRelation = new TLabelMemberRelation();
                                tLabelMemberRelation.setDeptId(item.getId());
                                tLabelMemberRelation.setLabelId(item1.getLabelId());
                                tLabelMemberRelationMapper.deleteByDeptIdAndMemIdAndLabelId(tLabelMemberRelation);
                                memeberList.forEach(item2 -> {
                                    TLabelMemberRelation tLabelMemberRelation1 = new TLabelMemberRelation();
                                    tLabelMemberRelation1.setDeptId(item.getId());
                                    tLabelMemberRelation1.setLabelId(item1.getLabelId());
                                    tLabelMemberRelation1.setMemberId(item2);
                                    tLabelMemberRelationMapper.insert(tLabelMemberRelation1);
                                });
                            });
                        }
                    }
                });
            }
            return RestResult.success(Boolean.TRUE);
        } catch (Exception e) {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "会员标签数据同步失败");
        }

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult<Boolean> getMemberInfoByAge() {
        List<SysDeptResponse> deptList = sysDeptApi.getAllDeptList();
        try {
            if (CollectionUtils.isNotEmpty(deptList)) {
                deptList.forEach(item -> {
                    //子平台母平台
                    if (item.getDeptType() == 0 || item.getDeptType() == 1) {
                        TLabel tLabel = new TLabel();
                        tLabel.setDeptId(item.getId());
                        tLabel.setLabelName("年龄");
                        List<TLabel> labelEnums = mapper.selectLabelEnumBySecondLabelName(tLabel);
                        labelEnums.forEach(item1 -> {
                            LabelRequest labelRequest = new LabelRequest();
                            labelRequest.setLabelType(4);
                            labelRequest.setDeptId(item.getId());
                            labelRequest.setLabelEnum(Long.valueOf(item1.getLabelEnum()));
                            //调用会员数据获取会员id
                            List<String> memeberList = mememberUserService.queryByLabelType(labelRequest);
                            //先删除已有的数据，重新保存
                            TLabelMemberRelation tLabelMemberRelation = new TLabelMemberRelation();
                            tLabelMemberRelation.setDeptId(item.getId());
                            tLabelMemberRelation.setLabelId(item1.getId());
                            tLabelMemberRelationMapper.deleteByDeptIdAndMemIdAndLabelId(tLabelMemberRelation);
                            memeberList.forEach(item2 -> {
                                TLabelMemberRelation tLabelMemberRelation1 = new TLabelMemberRelation();
                                tLabelMemberRelation1.setDeptId(item.getId());
                                tLabelMemberRelation1.setLabelId(item1.getId());
                                tLabelMemberRelation1.setMemberId(item2);
                                tLabelMemberRelationMapper.insert(tLabelMemberRelation1);
                            });
                        });
                    } else {
                        //商家店铺
                        TLabelMerchantRelation tLabelMerchantRelation = new TLabelMerchantRelation();
                        tLabelMerchantRelation.setDeptId(item.getId());
                        List<TLabelMerchantRelation> tLabelMerchantRelations = tLabelMerchantRelationMapper.selectBySelective(tLabelMerchantRelation);
                        if (CollectionUtils.isNotEmpty(tLabelMerchantRelations)) {
                            tLabelMerchantRelations.forEach(item1 -> {
                                LabelRequest labelRequest = new LabelRequest();
                                labelRequest.setLabelType(4);
                                labelRequest.setDeptId(item.getId());
                                labelRequest.setLabelEnum(Long.valueOf(item1.getLabelId()));
                                //调用会员数据获取会员id
                                List<String> memeberList = mememberUserService.queryByLabelType(labelRequest);
                                //先删除已有的数据，重新保存
                                TLabelMemberRelation tLabelMemberRelation = new TLabelMemberRelation();
                                tLabelMemberRelation.setDeptId(item.getId());
                                tLabelMemberRelation.setLabelId(item1.getLabelId());
                                tLabelMemberRelationMapper.deleteByDeptIdAndMemIdAndLabelId(tLabelMemberRelation);
                                memeberList.forEach(item2 -> {
                                    TLabelMemberRelation tLabelMemberRelation1 = new TLabelMemberRelation();
                                    tLabelMemberRelation1.setDeptId(item.getId());
                                    tLabelMemberRelation1.setLabelId(item1.getLabelId());
                                    tLabelMemberRelation1.setMemberId(item2);
                                    tLabelMemberRelationMapper.insert(tLabelMemberRelation1);
                                });
                            });
                        }
                    }
                });
            }
            return RestResult.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("会员标签数据同步失败:{}", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "会员标签数据同步失败");
        }

    }

    @Override
    public RestResult<Boolean> getMemberInfoByCustomLabel() {
        List<SysDeptResponse> deptList = sysDeptApi.getAllDeptList();
        try {
            if (CollectionUtils.isNotEmpty(deptList)) {
                deptList.forEach(item -> {
                    TLabel tLabel = new TLabel();
                    tLabel.setDeptId(item.getId());
                    tLabel.setLabelType(2);
                    List<TLabel> labelEnums = mapper.selectBySelective(tLabel);
                    labelEnums.forEach(item1 -> {
                        LabelRequest labelRequest = new LabelRequest();
                        labelRequest.setLabelType(6);
                        labelRequest.setDeptId(item.getId());
                        labelRequest.setLabelEnum(Long.valueOf(item1.getLabelEnum()));
                        //调用会员数据获取会员id
                        List<String> memeberList = mememberUserService.queryByLabelType(labelRequest);
                        //先删除已有的数据，重新保存
                        TLabelMemberRelation tLabelMemberRelation = new TLabelMemberRelation();
                        tLabelMemberRelation.setDeptId(item.getId());
                        tLabelMemberRelation.setLabelId(item1.getId());
                        tLabelMemberRelationMapper.deleteByDeptIdAndMemIdAndLabelId(tLabelMemberRelation);
                        if (CollectionUtils.isNotEmpty(memeberList)) {
                            memeberList.forEach(item2 -> {
                                TLabelMemberRelation tLabelMemberRelation1 = new TLabelMemberRelation();
                                tLabelMemberRelation1.setDeptId(item.getId());
                                tLabelMemberRelation1.setLabelId(item1.getId());
                                tLabelMemberRelation1.setMemberId(item2);
                                tLabelMemberRelation1.setDeptType(item.getDeptType());
                                tLabelMemberRelationMapper.insert(tLabelMemberRelation1);
                            });
                        }
                    });
                });
            }
            return RestResult.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("会员标签数据同步失败:{}", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "会员标签数据同步失败");
        }
    }

    /**
     * 商家店铺新增同步子平台标签数据
     *
     * @param deptId,merchantId
     * @return author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @Override
    public RestResult saveLabelMerchantOrStore(Long deptId, Long merchantOrStoreDeptId) {
        try {
            if (deptId != null && merchantOrStoreDeptId != null) {
                //根据deptId 查询所有已开放的二级标签
                TLabel tLabel = new TLabel();
                tLabel.setDeptId(deptId);
                tLabel.setStatus(1);
                tLabel.setLabelGrade(Long.valueOf(2));
                tLabel.setDeleteFlag(0);
                List<TLabel> labelList = mapper.selectBySelective(tLabel);
                if (CollectionUtils.isNotEmpty(labelList)) {
                    labelList.forEach(item -> {
                        TLabelMerchantRelation tLabelMerchantRelation = new TLabelMerchantRelation();
                        tLabelMerchantRelation.setDeptId(merchantOrStoreDeptId);
                        tLabelMerchantRelation.setLabelId(item.getId());
                        tLabelMerchantRelationMapper.insert(tLabelMerchantRelation);
                    });
                }
            } else {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "参数为空");
            }
        } catch (Exception e) {
            log.error("商家店铺新增同步子平台标签数据:{}", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "商家店铺新增同步子平台标签数据失败");
        }
        return RestResult.success(Boolean.TRUE);
    }

    /**
     * 自定义标签同步
     *
     * @param
     * @return
     * @auther
     * @date 2020-3-11 20:58:44
     */
    @Override
    public RestResult saveCustomLabel(HashMap map) {
        if (!map.isEmpty()) {
            if (map.get("type").toString().equals("1")) {
                //判断编辑还是新增
                TLabel tLabel1 = new TLabel();
                tLabel1.setLabelEnum(Long.valueOf(map.get("id").toString()));
                tLabel1.setDeptId(Long.valueOf(map.get("deptId").toString()));
                tLabel1.setLabelType(2);
                List<TLabel> labelList1 = mapper.selectBySelective(tLabel1);
                if (CollectionUtils.isNotEmpty(labelList1)) {
                    TLabel customLabel = new TLabel();
                    customLabel.setLabelName(map.get("levelName").toString());
                    customLabel.setId(labelList1.get(0).getId());
                    mapper.updateByPrimaryKey(customLabel);
                } else {
                    TLabel customLabel = new TLabel();
                    customLabel.setLabelName(map.get("levelName").toString());
                    customLabel.setStatus(2);
                    customLabel.setLabelGrade(Long.valueOf(3));
                    customLabel.setDeptId(Long.valueOf(map.get("deptId").toString()));
                    customLabel.setDeptType(Integer.valueOf(map.get("deptType").toString()));
                    customLabel.setLabelType(2);
                    customLabel.setLabelEnum(Long.valueOf(map.get("id").toString()));
                    mapper.insert(customLabel);
                }
            } else {
                //删除标签
                TLabel tLabel1 = new TLabel();
                tLabel1.setLabelEnum(Long.valueOf(map.get("id").toString()));
                tLabel1.setDeptId(Long.valueOf(map.get("deptId").toString()));
                tLabel1.setLabelType(2);
                List<TLabel> labelList1 = mapper.selectBySelective(tLabel1);
                if (CollectionUtils.isNotEmpty(labelList1)) {
                    TLabel customLabel = new TLabel();
                    customLabel.setId(labelList1.get(0).getId());
                    mapper.deleteByPrimaryKey(customLabel);
                }
            }

        }
        return RestResult.success(Boolean.TRUE);
    }


    @Override
    public PageResult<TLabelResponse> queryAllLabelByDeptId(TLabel request, Page page) {
        //计算下标
        int startIndex = (page.getStart() - 1) * page.getLimit();
        //所有标签
        List<TLabelResponse> list = mapper.listPageAllLabel(request, startIndex, page.getLimit());
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(item -> {
                if (null != item.getDeptId()) {
                    RestResult<String> restResult = sysDeptApi.queryDeptNameById(item.getDeptId());
                    if (restResult != null) {
                        item.setDeptName(restResult.getData());
                    }
                }
            });
        }
        Long count = mapper.listPageAllLabelCount(request);
        PageResult<TLabelResponse> result = new PageResult<>();
        result.setTotal(count);
        result.setRows(list);
        return result;
    }


    @Override
    public PageResult<TLabelResponse> queryAddedLabel(TLabelAddedRequest request, Page page) {
        //计算下标
        int startIndex = (page.getStart() - 1) * page.getLimit();
        //所有标签
        List<TLabelResponse> list = new ArrayList<>();
        Long count = 0L;
        if (request.getLabelIds().size() != 0) {
            list = mapper.listPageAddedLabel(request, startIndex, page.getLimit());
            if (CollectionUtils.isNotEmpty(list)) {
                list.forEach(item -> {
                    if (null != item.getDeptId()) {
                        RestResult<String> restResult = sysDeptApi.queryDeptNameById(item.getDeptId());
                        if (restResult != null) {
                            item.setDeptName(restResult.getData());
                        }
                    }
                });
            }
            count = mapper.listPageAddedLabelCount(request);
        }

        PageResult<TLabelResponse> result = new PageResult<>();
        result.setTotal(count);
        result.setRows(list);
        return result;
    }
}
